using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 控制角色移动

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    private Rigidbody2D rig;
    private Animator ani;
    private float inputX, inputY;
    private float stopX, stopY;

    // Start is called before the first frame update
    void Start()
    {
        rig = GetComponent<Rigidbody2D>();
        ani = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        inputX = Input.GetAxisRaw("Horizontal");
        inputY = Input.GetAxisRaw("Vertical");
        Vector2 input = (inputX*transform.right + inputY*transform.up).normalized;
        rig.velocity = input * speed;

        if(input != Vector2.zero){
            ani.SetBool("isMoving", true);
            stopX = inputX;
            stopY = inputY;
        }
        else{
            ani.SetBool("isMoving", false);
        }
        ani.SetFloat("inputX", stopX);
        ani.SetFloat("inputY", stopY);
    }
}
